package com.yejiali.backend.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.yejiali.backend.entity.dto.CommentDTO;
import com.yejiali.backend.entity.dto.CommentLikedDTO;
import com.yejiali.backend.entity.vo.CommentVO;
import com.yejiali.backend.response.Response;
import com.yejiali.backend.service.CommentLikeService;
import com.yejiali.backend.service.CommentsService;
import com.yejiali.backend.service.MentionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 评论表(Comments)表控制层
 *
 * @author yejiali
 * @since 2025-07-25 17:34:37
 */
@RestController
@RequestMapping("comments")
public class CommentsController{

    @Autowired
    private CommentsService commentsService;

    @Autowired
    private MentionsService mentionsService;

    @Autowired
    private CommentLikeService commentLikeService;

    /**
     * 多级评论(查询最新评论)
     */
    @GetMapping("/getCommentNewTree")
    public Response<List<CommentVO>> getCommentNewById(@RequestParam("videoId") Long videoId){
        return commentsService.getCommentNewById(videoId);
    }

    /**
     * 多级评论（查询最热评论）
     */
    @GetMapping("/getCommentHotTree")
    public Response<List<CommentVO>> getCommentHotById( @RequestParam("videoId") Long videoId,@RequestParam(required = false) Long userId){
        return commentsService.getCommentHotById(videoId,userId);
    }

    /**
     * 新增评论
     */
    @PostMapping("/addComment")
    public Response<String> addComment(@RequestBody CommentDTO commentDTO) throws JsonProcessingException {
        if(commentDTO.getHasAtUser()){
            return mentionsService.addAtMessage(commentDTO);
        }else{
            return commentsService.addComment(commentDTO);
        }
    }

    /**
     * 点赞某个评论
     */
    @PostMapping("/likeComments")
    public Response<String> likeComment(@RequestBody CommentLikedDTO commentLikedDTO) throws JsonProcessingException {
        return commentLikeService.likeComment(commentLikedDTO.getUserId(), commentLikedDTO.getCommentId());
    }

    /**
     * 取消点赞某个评论
     */
    @PostMapping("/cancelLikeComments")
    public Response<String> cancelLikeComment(@RequestBody CommentLikedDTO commentLikedDTO) {
        return commentLikeService.cancelLikeComment(commentLikedDTO.getUserId(), commentLikedDTO.getCommentId());
    }

}

